﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using arcchallenge.servicecontracts;

namespace arcchallenge.filewriter
{
    public class TextFileWriter : IFileWriter
    {
        internal class Configuration
        {
            public string defaultDirectoryname;
            public StreamWriter destination;
        }

        private readonly Configuration config;

        public TextFileWriter() : this(new Configuration()) {}
        internal TextFileWriter(Configuration config)
        {
            this.config = config;
        }

        public void In_Configure(string defaultDirectoryname)
        {
            if (defaultDirectoryname == "") defaultDirectoryname = ".";
            if (!defaultDirectoryname.EndsWith(@"\")) defaultDirectoryname += @"\";
            this.config.defaultDirectoryname = defaultDirectoryname;
        }


        public void In_WriteFile(string filename)
        {
            if (Path.GetDirectoryName(filename) == "")
                filename = this.config.defaultDirectoryname + filename;
            this.config.destination = new StreamWriter(filename, false, Encoding.Default);
        }


        public void In_WriteLine(string line)
        {
            if (line != null)
                this.config.destination.WriteLine(line);
            else
                this.config.destination.Close();
        }


        public void Dispose()
        {}
    }
}
